package com.freewater.javabase.demo.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class Transfer {
    public static void main(String[] args) throws IOException {
        transferFrom();
        transferTo();
    }

    private static void transferFrom() throws IOException {
        RandomAccessFile file = new RandomAccessFile("data/nio-data.txt", "rw");
        final FileChannel fromChannel = file.getChannel();
        RandomAccessFile toFile = new RandomAccessFile("data/toFile1.txt", "rw");
        final FileChannel toChannel = toFile.getChannel();
        toChannel.transferFrom(fromChannel, 0, fromChannel.size());
        toChannel.close();
        fromChannel.close();
    }

    private static void transferTo() throws IOException {
        RandomAccessFile file = new RandomAccessFile("data/nio-data.txt", "rw");
        final FileChannel fromChannel = file.getChannel();
        RandomAccessFile toFile = new RandomAccessFile("data/toFile2.txt", "rw");
        final FileChannel toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    }
}
